package com.jkzaixian.online;

import android.content.Intent;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.jkzaixian.online.video.VideoDetailActivity;

public class MyNativeModule extends ReactContextBaseJavaModule {

    public static final String REACTCLASSNAME = "MyNativeModule";
    private ReactApplicationContext mContext;

    public MyNativeModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return REACTCLASSNAME;
    }

    /**
     * 必须添加反射注解不然会报错
     * 这个方法就是ReactNative将要调用的方法，会通过此类名字调用
     *
     */
    @ReactMethod
    public void callNativeMethod() {
//        toSelectArea();
        Intent intent = new Intent(mContext, AddressActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    @ReactMethod
    public void callPlayMethod() {
        Intent intent = new Intent(mContext, VideoDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

//    /**
//     * js页面跳转到activity 并传数据
//     */
//    @ReactMethod
//    public void startActivityByClassname(String activityName, int requestCode, Callback successCallback, Callback erroCallback) {
//        try {
//            Activity currentActivity = getCurrentActivity();
//            if (null != currentActivity) {
//                Class aimActivity = Class.forName(activityName);
//                Intent intent = new Intent(currentActivity, aimActivity);
//                currentActivity.startActivityForResult(intent,requestCode);
//                String result= (String) MyConstants.myBlockingQueue.take();
//                successCallback.invoke(result);
//            }
//        } catch (Exception e) {
//            throw new JSApplicationIllegalArgumentException(
//                    "无法打开activity页面: " + e.getMessage());
//        }
//    }

    /**
     * 向RN发送消息
     * @param params
     */
    public void sendMessage(@Nullable WritableMap params){
        mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("AndroidToRNMessage",params);
    }

}
